package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.cart.CartIndexVo;
import com.cskaoyan.bean.cart.CartTotalDTO;
import com.cskaoyan.mapper.CartMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.GoodsProductMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class CartServiceImpl_ implements CartService_ {

    @Autowired
    CartMapper cartMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;


    public Double getAmount(List<Cart> carts) {
        Double sum = 0.0;
        for (int i = 0; i < carts.size(); i++) {
            sum += carts.get(i).getPrice().doubleValue() * carts.get(i).getNumber();
        }
        return sum;
    }

    public Integer getCount(List<Cart> carts) {
        Integer count = 0;
        for (int i = 0; i < carts.size(); i++) {
            count += carts.get(i).getNumber();
        }
        return count;
    }

    @Override
    public CartIndexVo cartIndex() {
        Subject subject = SecurityUtils.getSubject();
        if(subject.getPrincipals()==null){
            return null;
        }
        CartIndexVo vo = new CartIndexVo();
        CartTotalDTO cartTotal = new CartTotalDTO();
        CartExample example = new CartExample();
        CartExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(getUserId());
        criteria.andCheckedEqualTo(true);
        criteria.andDeletedEqualTo(false);
        List<Cart> checkedCarts = cartMapper.selectByExample(example);

        cartTotal.setCheckedGoodsCount(getCount(checkedCarts));
        cartTotal.setCheckedGoodsAmount(getAmount(checkedCarts));

        CartExample example1 = new CartExample();
        CartExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andUserIdEqualTo(getUserId());
        criteria1.andDeletedEqualTo(false);
        List<Cart> carts = cartMapper.selectByExample(example1);

        cartTotal.setGoodsCount(getCount(carts));
        cartTotal.setGoodsAmount(getAmount(carts));

        vo.setCartTotal(cartTotal);
        vo.setCartList(carts);
        return vo;
    }

    @Override
    public long cartGoodscount() {
        Subject subject = SecurityUtils.getSubject();
        if(subject.getPrincipals()==null){
            return 0;
        }
        CartExample example = new CartExample();
        CartExample.Criteria criteria = example.createCriteria();

        criteria.andUserIdEqualTo(getUserId());
        criteria.andDeletedEqualTo(false);
        List<Cart> carts = cartMapper.selectByExample(example);
        return getCount(carts);
    }

    @Override
    public void addCart(Cart cart) {
        Subject subject = SecurityUtils.getSubject();
        if(subject.getPrincipals()==null){
            return;
        }
        cart.setUserId(getUserId());
        Integer goodsId = cart.getGoodsId();
        CartExample cartExample = new CartExample();
        CartExample.Criteria criteria = cartExample.createCriteria();
        criteria.andUserIdEqualTo(getUserId());
        criteria.andGoodsIdEqualTo(goodsId);
        criteria.andDeletedEqualTo(false);
        List<Cart> carts = cartMapper.selectByExample(cartExample);
        if(carts!=null&&carts.size()>0){
            //加入数量
            Integer addNum=cart.getNumber();
            //之前的数量
            Integer preNum = carts.get(0).getNumber();
            carts.get(0).setNumber(addNum+preNum);
            carts.get(0).setUpdateTime(new Date());
            cartMapper.updateByPrimaryKey(carts.get(0));
            return;
        }
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
        cart.setGoodsSn(goods.getGoodsSn());
        cart.setGoodsName(goods.getName());
        cart.setPrice(goods.getRetailPrice());
        GoodsProduct goodsProduct = goodsProductMapper.selectByPrimaryKey(cart.getProductId());
        cart.setSpecifications(goodsProduct.getSpecifications());
        cart.setPicUrl(goodsProduct.getUrl());
        cart.setAddTime(new Date());
        cart.setUpdateTime(new Date());
        cartMapper.insertSelective(cart);


    }

    @Override
    public void cartUpdate(Cart cart) {
        CartExample example = new CartExample();
        CartExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(cart.getId());
        criteria.andDeletedEqualTo(false);
        List<Cart> carts = cartMapper.selectByExample(example);
        Cart updateCart = carts.get(0);
        updateCart.setUpdateTime(new Date());
        updateCart.setNumber(cart.getNumber());
        cartMapper.updateByPrimaryKey(updateCart);
    }

    private Integer getUserId() {
        Subject subject = SecurityUtils.getSubject();
        Object primaryPrincipal = subject.getPrincipals().getPrimaryPrincipal();
        return ((User) primaryPrincipal).getId();

    }
}
